# -*- cmake -*-


# - Find cryptlib
# Find the cryptlib includes and libraries
# This module defines
#  CRYPTLIB_INCLUDE_DIR, where to find cryptlib.h, etc.
#  CRYPTLIB_LIBRARIES, the libraries needed for cryptlib.
#  CRYPTLIB_FOUND, If false, do not try to use cryptlib.
# also defined, but not for general use are
#  CRYPTLIB_LIBRARY, where to find cryptlib.

FIND_PATH(CRYPTLIB_INCLUDE_DIR cryptlib.h)

SET(CRYPTLIB_NAMES ${CRYPTLIB_NAMES} cl)
FIND_LIBRARY(CRYPTLIB_LIBRARY
  NAMES ${CRYPTLIB_NAMES}
  PATHS /usr/lib /usr/local/lib
  )

IF (CRYPTLIB_LIBRARY AND CRYPTLIB_INCLUDE_DIR)
    SET(CRYPTLIB_LIBRARIES ${CRYPTLIB_LIBRARY})
    SET(CRYPTLIB_FOUND "YES")
ELSE (CRYPTLIB_LIBRARY AND CRYPTLIB_INCLUDE_DIR)
  SET(CRYPTLIB_FOUND "NO")
ENDIF (CRYPTLIB_LIBRARY AND CRYPTLIB_INCLUDE_DIR)

if (CRYPTLIB_INCLUDE_DIR)
  file(STRINGS "${CRYPTLIB_INCLUDE_DIR}/cryptlib.h" cryptlib_version_str REGEX "^#define[\t ]+CRYPTLIB_VERSION[\t ]+[0-9][0-9][0-9][0-9].*")

  string(REGEX REPLACE "^.*CRYPTLIB_VERSION[\t ]+([0-9]).*$" "\\1" CRYPTLIB_VERSION_MAJOR "${cryptlib_version_str}")
  string(REGEX REPLACE "^.*CRYPTLIB_VERSION[\t ]+[0-9]([0-9]).*$" "\\1" CRYPTLIB_VERSION_MINOR  "${cryptlib_version_str}")
  string(REGEX REPLACE "^.*CRYPTLIB_VERSION[\t ]+[0-9][0-9]([0-9][0-9]).*$" "\\1" CRYPTLIB_VERSION_PATCH "${cryptlib_version_str}")

  string(REGEX REPLACE "^0(.+)" "\\1" CRYPTLIB_VERSION_MAJOR "${CRYPTLIB_VERSION_MAJOR}")
  string(REGEX REPLACE "^0(.+)" "\\1" CRYPTLIB_VERSION_MINOR "${CRYPTLIB_VERSION_MINOR}")
  string(REGEX REPLACE "^0(.+)" "\\1" CRYPTLIB_VERSION_PATCH "${CRYPTLIB_VERSION_PATCH}")

  set(CRYPTLIB_VERSION "${CRYPTLIB_VERSION_MAJOR}.${CRYPTLIB_VERSION_MINOR}.${CRYPTLIB_VERSION_PATCH}")
endif (CRYPTLIB_INCLUDE_DIR)

IF (CRYPTLIB_FOUND)
   MESSAGE(STATUS "Found cryptlib ${CRYPTLIB_VERSION}: ${CRYPTLIB_LIBRARIES}")
ELSE (CRYPTLIB_FOUND)
   IF (CRYPTLIB_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find cryptlib library")
   ENDIF (CRYPTLIB_FIND_REQUIRED)
ENDIF (CRYPTLIB_FOUND)

MARK_AS_ADVANCED(
  CRYPTLIB_LIBRARY
  CRYPTLIB_INCLUDE_DIR
  )
